package com.lib;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static <Student> void main(String[] args) throws Exception{
        //连接数据库  MySQL8.0及以上操作
        /*
        * MySQL5.0----->Class.forName("com.mysql.jdbc.Driver");
        *
        * */
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/kh62";
        String userName = "root";
        String uPassword = "gzx123zyl920";

        //数据库连接对象
        Connection connection = DriverManager.getConnection(url, userName, uPassword);
        System.out.println(connection);

        //执行sql
        String sql = "select * from student";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        //处理返回结果集
        List<com.lib.Student> studentList = new ArrayList<>();
        while(resultSet.next()){
            com.lib.Student student = new com.lib.Student();
            student.setId(resultSet.getInt("StudentNo"));
            student.setName(resultSet.getString("StudentName"));
            student.setPassword(resultSet.getString("LoginPwd"));
            studentList.add(student);
        }

        resultSet.close();
        preparedStatement.close();
        connection.close();

        for (com.lib.Student student : studentList){
            System.out.println(student.toString());
        }

    }
}
